﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using myMvcBlog.Models;

namespace myMvcBlog.Infrastructure
{
	public static class Extensions
	{
		public static IList<PostView> ToPostViews(this IQueryable<Post> posts)
		{
            IList<PostView> postViews = new List<PostView>();
            foreach (var post in posts)
            {
                IList<Tag> tags = (from pt in post.PostTags
                                   join t in Tag.All() on pt.TagID equals t.ID
                                   select t).ToList();

                IList<Category> categories = (from pc in post.PostCategories
                                              join c in Category.All() on pc.CategoryID equals c.ID
                                              select c).ToList();

                PostView pv = new PostView()
                {
                    Post = post,
                    Categories = categories,
                    Tags = tags,
                    Comments = post.Comments.ToList(),
                };

                postViews.Add(pv);
            }

            return postViews;
		}
	}
}